type emitItem = {
	onShow:Function,
	onHide:Function,
	onUnload:Function,
	onReachBottom:Function,
	onPullDownRefresh:Function,
	onPageScroll:Function
}
class customTabbar {
	emits = new Map<string,emitItem>([]);
	on({
		key = '',
		onShow = () => {},
		onHide = () => {},
		onUnload = () => {},
		onReachBottom = () => {},
		onPullDownRefresh = () => {},
		onPageScroll = () => {}
	}) {
		this.emits.set(key, {
			onShow,
			onHide,
			onUnload,
			onReachBottom,
			onPullDownRefresh,
			onPageScroll
		})
	}
	off(key:string){
		this.emits.delete(key)
	}
	onShow(key:string){
		if (this.emits.has(key)) {
			this.emits.get(key)?.onShow()
		}
		// onShow时，需设置其他页面为hide状态
		this.onHide(key)
	}
	onHide(key?:string){
		for(let _key of this.emits.keys()){
			if(key !== _key){
				this.emits.get(_key)?.onHide()
			}
		}
		

	}
	onUnload(key:string){
		if (this.emits.has(key)) {
			this.emits.get(key)?.onUnload()
		}
	}
	onReachBottom(key:string){
		if (this.emits.has(key)) {
			this.emits.get(key)?.onReachBottom()
		}
	}
	onPullDownRefresh(key:string){
		if (this.emits.has(key)) {
			this.emits.get(key)?.onPullDownRefresh()
		}
	}
	onPageScroll(key:string){
		if (this.emits.has(key)) {
			this.emits.get(key)?.onPageScroll()
		}
	}
}

const _customTabbar = new customTabbar()

export const useTabbar = (key:string) => {
	const emits = {
		onShow(){},
		onHide(){},
		onUnload(){},
		onReachBottom(){},
		onPullDownRefresh(){},
		onPageScroll(){}
	}
	const onShow = (cb:Function) => {
		emits.onShow = () => {cb()}
		_customTabbar.on({key,...emits})
	}
	const onHide =(cb:Function) => {
		emits.onHide = () => {cb()}
		_customTabbar.on({key,...emits})
	}
	const onUnload =(cb:Function) => {
		emits.onUnload = () => {cb()}
		_customTabbar.on({key,...emits})
	}
	const onReachBottom = (cb:Function) => {
		emits.onReachBottom = () => {cb()}
		_customTabbar.on({key,...emits})
	}
	const onPullDownRefresh =(cb:Function) => {
		emits.onPullDownRefresh = () => {cb()}
		_customTabbar.on({key,...emits})
	}
	const onPageScroll =(cb:Function) => {
		emits.onPageScroll = () => {cb()}
		_customTabbar.on({key,...emits})
	}
	return {
		onShow,
		onHide,
		onUnload,
		onReachBottom,
		onPullDownRefresh,
		onPageScroll
	}
}

export default _customTabbar
